﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using road2.code;
using road2.Views;
using road2.Model;

namespace road2
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        DispatcherTimer timer = new DispatcherTimer();

        List<CrossRoadClass> crCollection = new List<CrossRoadClass>();
        List<RoadClass> roadCollection = new List<RoadClass>();
        List<CarClass> carCollection = new List<CarClass>();
        List<TrafficGeneratorClass> tgCollection = new List<TrafficGeneratorClass>();
        List<TrafficTerminatorClass> ttCollection = new List<TrafficTerminatorClass>();

        List<RoadUserControl> roadUCCollection = new List<RoadUserControl>();

        //RoadClass road, road2;
        //RoadUserControl roadUC, road2UC;

        public MainWindow()
        {
            InitializeComponent();

            testButton.Click += new RoutedEventHandler(testButton_Click);
            test2Button.Click += new RoutedEventHandler(test2Button_Click);
        }

        Road road;
        Car car;

        void test2Button_Click(object sender, RoutedEventArgs e)
        {
            car.Drive();
        }

        void testButton_Click(object sender, RoutedEventArgs e)
        {
            road = new Road(Guid.NewGuid(), 500, 2);

            car = new Car(Guid.NewGuid(), CarTypes.small, 0, 20, 10, -10);
            car.road = road;

            CarPanel carp = new CarPanel();
            carp.DataContext = car;

            PropertiesGrid.Children.Clear();
            PropertiesGrid.Children.Add(carp);
        }

        #region init

        private void Init()
        {
            InitTimer();
            InitRoads();
            InitTrafficGen();
            InitTrafficTerm();
        }

        private void InitTimer()
        {
            timer.Interval = new TimeSpan(0, 0, 0, 0, 1000);

            timer.Tick += new EventHandler(timer_Tick);

            //TimerControl.DataContext = timer;
        }

        void timer_Tick(object sender, EventArgs e)
        {
            foreach (var tg in tgCollection)
            {
                var newCar = tg.GenCar(carCollection);

                if (newCar != null)
                {
                    carCollection.Add(newCar);
                    //roadUC.AddCar(newCar);
                    var roadUC = roadUCCollection.RoadUCbyRoad(tg.Road);
                    if (roadUC != null)
                    {
                        roadUC.AddCar(newCar);
                    }

                    //Application.Current.MainWindow.Title = "car count:" + carCollection.Count().ToString();
                }
            }
            
            foreach (var car in carCollection)
            {
                CarRoadManager.DriveCar(car, carCollection, roadCollection, ttCollection);
            }

            foreach (var car in carCollection.Where(c => c.Road == null)) // fix here
            {
                //roadUC.RemoveCar(car);

                foreach (var road in roadCollection) // and here
                {
                    roadUCCollection.RoadUCbyRoad(road).RemoveCar(car);
                }
            }
        }

        private void InitRoads()
        {
            roadCollection.Add(new RoadClass(Guid.NewGuid(), 500, 3, 16));
            //roadCollection.Add(new RoadClass(400, 4, 20));
        }

        private void InitTrafficGen()
        {
            var tg = new TrafficGeneratorClass(Guid.NewGuid(), roadCollection[0]);
            tg.VPI = 0.5;
            //VpiControl.DataContext = tg;

            tgCollection.Add(tg);
        }

        private void InitTrafficTerm()
        {
            var tt = new TrafficTerminatorClass(Guid.NewGuid(), roadCollection[0]);

            ttCollection.Add(tt);
        }

        #endregion

        #region drawing

        private void DrawRoads(IEnumerable<RoadClass> roads, Canvas canvas)
        {
            foreach (var road in roads)
            {
                DrawRoad(road, canvas);
            }
        }

        private void DrawRoad(RoadClass road, Canvas canvas)
        {
            RoadUserControl roadUC = new RoadUserControl(road, carCollection.Where(c => c.Road == road));
            roadUCCollection.Add(roadUC);
            canvas.Children.Add(roadUC);

            UpdateLayout();

            //road2UC = new RoadUserControl(road2, carCollection.Where(c => c.Road == road2));
            //road2UC.SetValue(Canvas.TopProperty, roadUC.RoadRect.Height + 10);
            //canvas1.Children.Add(road2UC);
            //UpdateLayout();
            //road2UC.RenderTransform = new RotateTransform(180, road2UC.RoadRect.Width / 2, road2UC.RoadRect.Height / 2);
            
        }

        #endregion

        #region buttons

        private void InitButton_Click(object sender, RoutedEventArgs e)
        {
            Init();
        }

        private void RoadButton_Click(object sender, RoutedEventArgs e)
        {
            //DrawRoads(roadCollection, canvas1);
        }

        private void CarButton_Click(object sender, RoutedEventArgs e)
        {
            //MessageBox.Show(tg.VPI.ToString());
        }

        private void GoButton_Click(object sender, RoutedEventArgs e)
        {
            timer.IsEnabled = !timer.IsEnabled;
            //GoButton.Content = timer.IsEnabled ? "Turn off" : "Turn on";
        }

        #endregion
    }
}
